package com.lvmama.rhino.service.impl.comment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lvmama.rhino.service.comment.RedisService;
import com.lvmama.rhino.utils.Constants;
import com.lvmama.rhino.utils.DateUtil;
import com.lvmama.rhino.utils.RhinoConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

@Service
@Transactional
public class RedisServiceImpl implements RedisService {

    private static Jedis getJedis() throws Exception {
        RhinoConstant rhinoConstant = RhinoConstant.getInstance();
        String JedisUrl = rhinoConstant.getValue("JedisUrl");
        String JedisPort = rhinoConstant.getValue("JedisPort");
        Jedis jedis = new Jedis(JedisUrl, Integer.parseInt(JedisPort));
        return jedis;
    }

    @Override
    public String setValueExpire(String key, Object value, int seconds) throws Exception {
        Jedis jedis = getJedis();
        String result;
        Class clazz = value.getClass();
        if (clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Double.class) ||
                clazz.equals(Float.class) || clazz.equals(Short.class) || clazz.equals(Long.class) ||
                clazz.equals(Boolean.class)) {
            result = jedis.setex(key, seconds, value.toString());
        } else {
            Gson gson = new Gson();
            String obj = gson.toJson(value);
            result = jedis.setex(key, seconds, obj);
        }
        jedis.close();
        return result;
    }

//    @Override
//    public Object getListValue(String key) throws Exception {
//        Jedis jedis = getJedis();
//        String result = jedis.get(key);
//        if (StringUtils.isBlank(result)) {
//            return null;
//        }
//        Gson gson = new Gson();
//        List list = gson.fromJson(result, new TypeToken<List>() {}.getType());
//        jedis.close();
//        return list;
//    }

    @Override
    public String getObjectValue(String key) throws Exception {
        Jedis jedis = getJedis();
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

    @Override
    public byte[] getByteArrayValue(byte[] bytes) throws Exception {
        Jedis jedis = getJedis();
        byte[] result = jedis.get(bytes);
        jedis.close();
        return result;
    }

    @Override
    public String getKey(String prefix) throws Exception {
        return Constants.KEY_PREFIX + prefix + "_" + DateUtil.getToday("yyyyMMdd");
    }

    @Override
    public void deleteKeys(String prefix) throws Exception {
        Jedis jedis = getJedis();
        Set<String> keys = jedis.keys(prefix + "*");
        for (String key : keys) {
            jedis.del(key);
        }
        jedis.close();
    }

}
